"""
ftp文件服务器客户端
"""
filedir="D:\\Python人工智能\\mouth02\\day_09\\new_files\\"
import sys
import os
from time import sleep
from socket import *
SERVER_ADDR=("127.0.0.1",8888)
class Handle():
    def __init__(self):
        self.sock=self._return_sock()
        self.status={
            "OK":"文件开始下载",
            "FAIL": "文件不存在",
            "EXIT": "文件已存在",
            "EMPTY": "文件库为空",
        }
    def _return_sock(self):
        sock=socket()
        sock.connect(SERVER_ADDR)
        return sock
    def do_get(self,filename):
        msg="Get "+filename
        self.sock.send(msg.encode())
        response=self.sock.recv(1024).decode()
        if response=="ok":
            fw=open(filename,"wb")
            while True:
                data=self.sock.recv(1024)
                if data==b"##":
                    break
                fw.write(data)
            fw.close()
        elif response=="FAIL":
            print(self.status[response])

    def do_quit(self):
        self.sock.send(b"Quit")
        self.sock.close()
        sys.exit("谢谢使用，拜拜！")

    def do_put(self, filename):
        try:
            fw = open(filedir + filename, "rb")
        except:
            print("文件不存在")
        file=filename.split(r"\\")[-1]     #剔除文件目录路径，获取文件名
        msg="Put "+filename
        self.sock.send(msg.encode())
        resp=self.sock.recv(1024).decode()
        if resp=='OK':
            fw=open(filedir+filename,"rb")
            while True:
                data=fw.read(1024)
                if not data:
                    break
                self.sock.send(data)
            sleep(0.1)
            self.sock.send(b"##")
            self.sock.close()
        elif resp=="EXIT":
            print(self.status[resp])

    def do_list(self):
        msg = b"List "
        self.sock.send(msg)
        response = self.sock.recv(1024).decode()
        if response == "OK":

            data=self.sock.recv(1024*1024)
            print(data.decode())

        elif response == "FAIL":
            print(self.status[response])


class FTPView():
    def __init__(self):
        self.handle=Handle()
    def _display_menu(self):
        print("""
        ==============FTP SERVER=========
        1.查看 2.下载 3.上传 4.退出
        """)
    def _select(self):
        while True:
            cmd=input("请输入你的选项：")
            if cmd=="1":
                self.handle.do_list()
            elif cmd=="2":
                filename=input("请输入你要下载的文件名：")
                self.handle.do_get(filename)
            elif cmd=="3":
                filepath = input("请输入你要上传的文件路径：")
                if os.path.exists(filepath):
                    self.handle.do_put(filepath)
                else:
                    print("文件不存在，请重新上传")


            elif cmd=="4":
                self.handle.do_quit()
            else:
                pass

    def main(self):
        while True:
            self._display_menu()
            self._select()

if __name__ == '__main__':
    ftp=FTPView()
    ftp.main()


